const UeSdk = require('../../models/ueSdk');

class UeSdkModule {
  // 创建SDK文档
  async createSdk(sdkData) {
    try {
      console.log('创建SDK文档:', sdkData); 
      const sdk = new UeSdk(sdkData);
      const savedSdk = await sdk.save();
      return {
        code: 200,
        status: 'success',
        message: '创建SDK文档成功',
        data: savedSdk
      };
    } catch (error) {
      throw new Error(`创建SDK文档失败: ${error.message}`);
    }
  }

  // 更新SDK文档
  async updateSdk(data) {
    try {
      const { id, ...updateData } = data;
      const sdk = await UeSdk.findByIdAndUpdate(
        id,
        updateData,
        { new: true, runValidators: true }
      );
      if (!sdk) {
        throw new Error('SDK文档不存在');
      }
      return {
        code: 200,
        status: 'success',
        message: '更新SDK文档成功',
        data: sdk
      };
    } catch (error) {
      throw new Error(`更新SDK文档失败: ${error.message}`);
    }
  }

  // 删除SDK文档
  async deleteSdk(data) {
    try {
      const { id } = data;
      const sdk = await UeSdk.findByIdAndDelete(id);
      if (!sdk) {
        throw new Error('SDK文档不存在');
      }
      return {
        code: 200,
        status: 'success',
        message: '删除SDK文档成功',
        data: sdk
      };
    } catch (error) {
      throw new Error(`删除SDK文档失败: ${error.message}`);
    }
  }

  // 获取SDK文档详情
  async getSdkDetail(data) {
    try {
      const { id } = data;
      const sdk = await UeSdk.findById(id);
      if (!sdk) {
        throw new Error('SDK文档不存在');
      }
      return {
        code: 200,
        status: 'success',
        message: '获取SDK文档详情成功',
        data: sdk
      };
    } catch (error) {
      throw new Error(`获取SDK文档详情失败: ${error.message}`);
    }
  }

  // 获取SDK文档列表（按大类分组）
  async getSdkList() {
    try {
      // 获取所有SDK文档，只返回关键信息
      const sdks = await UeSdk.find({}, {
        sdkCategory: 1,
        categoryName: 1,
        method: 1,
        methodName: 1,
      });

      // 按SDK大类分组并转换格式
      const groupedSdks = sdks.reduce((acc, sdk) => {
        const category = sdk.sdkCategory;
        let categoryGroup = acc.find(item => item.sdkCategory === category);
        
        if (!categoryGroup) {
          categoryGroup = {
            sdkCategory: category,
            categoryName: sdk.categoryName,
            methodsList: []
          };
          acc.push(categoryGroup);
        }
        
        categoryGroup.methodsList.push({
          method: sdk.method,
          methodName: sdk.methodName,
          _id: sdk._id
        });
        
        return acc;
      }, []);

      return {
        code: 200,
        status: 'success',
        message: '获取SDK文档列表成功',
        data: groupedSdks
      };
    } catch (error) {
      throw new Error(`获取SDK文档列表失败: ${error.message}`);
    }
  }

  // 搜索SDK文档
  async searchSdk(data) {
    try {
      const { sdkCategory, method, keyword } = data;
      const searchCondition = {};
      
      // 构建搜索条件
      if (sdkCategory) {
        searchCondition.sdkCategory = sdkCategory;
      }
      if (method) {
        searchCondition.method = { $regex: method, $options: 'i' };
      }
      if (keyword) {
        searchCondition.$or = [
          { methodName: { $regex: keyword, $options: 'i' } },
          { description: { $regex: keyword, $options: 'i' } }
        ];
      }

      const sdks = await UeSdk.find(searchCondition, {
        sdkCategory: 1,
        categoryName: 1,
        method: 1,
        methodName: 1,
        description: 1
      });

      return {
        code: 200,
        status: 'success',
        message: '搜索SDK文档成功',
        data: sdks
      };
    } catch (error) {
      throw new Error(`搜索SDK文档失败: ${error.message}`);
    }
  }
}

module.exports = new UeSdkModule();